/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gem.gui.courses;

import gem.model.core.Department;
import gem.model.core.ScienceDomain;
import gem.model.core.Course;
import gem.model.core.DBEntity;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 *
 * @author Dusan
 */
public class CoursesTreeModel implements TreeModel
{
    //private java.util.Collection< Department > deps;
    private java.util.Collection< DBEntity > entities = null;
    

    public CoursesTreeModel setContent( java.util.Collection< DBEntity > entities )
    {
        this.entities = entities;
        return this;
    }
    
    public Object getRoot()
    {
        return new DBEntity()
        {
            public EntityType getType()
            {
                return EntityType.NONE;
            }

            public String getCaption()
            {
                return "Katedre";
            }
            @Override
            public String toString()
            {
                return "Katedre";
            }
        };
    }

    public Object getChild(Object parent, int index)
    {
        DBEntity ent = (DBEntity)parent;
       
        switch( ent.getType() )
        {
        case NONE:
            return entities.toArray()[index];
        case DEPARTMENT:
            return((Department)ent).getScienceDomains().toArray()[ index ];
        case SCIENCEDOMAIN:
            return ((ScienceDomain)parent).getCourses().toArray()[ index ];
        }

        return null;
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getChildCount(Object parent)
    {
        DBEntity ent = (DBEntity)parent;
        
        if( ent.getType() == DBEntity.EntityType.NONE )
        {
            return entities.size();
        }        

        if( ent.getType() == DBEntity.EntityType.DEPARTMENT )
        {
            return ((Department)ent).getScienceDomains().size();
        }
        else
        {
            return ((ScienceDomain)ent).getCourses().size();
        }
    }

    public boolean isLeaf(Object node)
    {
        DBEntity ent = (DBEntity)node;
        return ( ent.getType() == DBEntity.EntityType.COURSE );
    }

    public void valueForPathChanged(TreePath path, Object newValue)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getIndexOfChild(Object parent, Object child)
    {
        DBEntity ent = (DBEntity)parent;
        
        if ( ent.getType() == DBEntity.EntityType.NONE )
        {
           Department dep = (Department)child;
           int i = 0;
           for( DBEntity d : entities )
           {
               if( d.equals(dep))
               {
                   return i;
               }
               i++;
           }
        }
        else if( ent.getType() == DBEntity.EntityType.DEPARTMENT )
        {
            int i  = 0;
            for( ScienceDomain sd : (((Department)ent).getScienceDomains()) )
            {
                if( sd.equals(child))
                {
                    return i;
                }
                i++;
            }
        }
        else if( ent.getType() == DBEntity.EntityType.SCIENCEDOMAIN )
        {
            int i = 0;
            for( Course c : (((ScienceDomain)ent).getCourses()))
            {
                if( c.equals(child))
                {
                    return i;
                }
                i++;
            }
        }
        return -1;
    }

    public void addTreeModelListener(TreeModelListener l)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public void removeTreeModelListener(TreeModelListener l)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
